const utils           = require('../../../utils');
const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class LoginHandler extends BaseHandler {

    get pattern() {
        return "/login";
    }

    doGET(request, response) {
        var session = request.session;
        var security_code = utils.uuid();
        var query = request.query;
        this.ensureRef(request);
        var logo = '/dashboard/images/sign-in-logo.png'
        var host = request.headers['host'];
        if (['localhost:3000', 'onwall-ppe.parllay.cn', 'billboards.parllay.cn'].indexOf(host) < 0) {
            logo = `/oem/${host}/images/login-logo.png`;
        }
        if(session && session.user && session.user.authenticated){
            response.redirect('/console');
        } else {
            request.session.user.security_code = security_code;
            response.render('console/login', {
                success: true,
                is_login: true,
                security_code: security_code,
                data: {},
                logo: logo
            });
        }
    }

    doPOST(request, response) {
        var me = this;
        var logo = '/dashboard/images/sign-in-logo.png'
        var host = request.headers['host'];
        if (['localhost:3000', 'onwall-ppe.parllay.cn', 'billboards.parllay.cn'].indexOf(host) < 0) {
            logo = `/oem/${host}/images/login-logo.png`;
        }
        var security_code = utils.uuid();
        if(!request.body.security_code || request.body.security_code !== request.session.user.security_code) {
            request.session.user.security_code = security_code;
            return response.render('console/login', {
                success: false,
                is_login: true,
                error: request.t('login.errors.invalid_request'),
                security_code: security_code,
                logo: logo,
                data: {}
            });
        }
        var email = (request.body.email || '').trim();
        var password = (request.body.password || '').trim();
        if(!email || !password) {
            request.session.user.security_code = security_code;
            response.render('console/login', {
                success: false,
                error: request.t('login.errors.email_or_password_empty'),
                is_login: true,
                security_code: request.session.user.security_code,
                logo: logo,
                data: request.body
            });
        } else {
            this.Controller.verifyPassword(email, password, (result) => {
                if(result.success) {
                    delete request.session.user.security_code
                    request.session.user.authenticated = true;
                    request.session.user.id = result.data.id;
                    request.session.user.name = result.data.name;
                    request.session.user.profile_image = me.Controller.getCDNImageURL(result.data.profile_image);
                    request.session.user.hash_id = result.data.hash_id;
                    request.session.user.ref = result.data.ref;
                    request.session.user.qr_code = result.data.qr_code;
                    response.redirect('/console');
                } else {
                    result.error = request.t(result.error);
                    result.is_login = true;
                    request.session.user.security_code = security_code;
                    result.security_code = request.session.user.security_code;
                    result.data = request.body;
                    result.logo = logo;
                    response.render('console/login', result);
                }
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = LoginHandler;